#import "DDZFManager.h"
#import "DDRequest.h"
#import "DDZFModel.h"
#import "DDFSModel.h"
#import "DDZFViewController.h"
#import "TCCIAP.h"
@interface DDZFManager()<JTWRequestDelegate>
@property (nonatomic, strong) DDRequest *request;
@property (nonatomic, strong) DDZFModel *model;
@property (nonatomic, strong) DDProduct *product;
@property (nonatomic, strong) managerBlock webBlock;
@end
@implementation DDZFManager
static DDZFManager * _JTWZFManager;
static dispatch_once_t JTWZFManageronceToken;
+ (instancetype)manager
{
    if (!_JTWZFManager)
    {
        dispatch_once(&JTWZFManageronceToken,^{
            _JTWZFManager = [[DDZFManager alloc]init];
            [CCNotice addObs:_JTWZFManager sel:@selector(dd_actionIAP:) name:TCCIAPNotification obj:nil];
        });
    }
    return  _JTWZFManager;
}
- (DDRequest *)request
{
    if (!_request)
    {
        _request = [[DDRequest  alloc] init];
        _request.delegate = self;
    }
    return _request;
}
+ (void)dd_managerZF:(DDProduct *)product web:(managerBlock)web_block
{
    _JTWZFManager = [DDZFManager manager];
    _JTWZFManager.webBlock = web_block;
    [CCHud startHud];
    //发起支付
    [_JTWZFManager.request dd_zfRequest:_JTWZFManager.product];
}
+ (void)dd_managerPrepare:(DDProduct *)product
{
    if (!product.goodsID || [product.goodsID length] < 1) return [CCAlert showMessage:@"请填写商品内购ID" hidden:nil];
    else if (!product.goodsTitle || [product.goodsTitle length] < 1) return [CCAlert showMessage:@"请填写商品标题" hidden:nil];
    else if (!product.goodsPrice || [product.goodsPrice length] < 1) return [CCAlert showMessage:@"请填写商品价格" hidden:nil];
    else if (!product.notifyURL || [product.notifyURL length] < 1) return [CCAlert showMessage:@"请填写后台支付通知接口" hidden:nil];
    else if (!product.serverID || [product.serverID length] < 1) return [CCAlert showMessage:@"请填写游戏区服ID" hidden:nil];
    else if (!product.serverName || [product.serverName length] < 1) return [CCAlert showMessage:@"请填写游戏区服名称" hidden:nil];
    else if (!product.roleID || [product.roleID length] < 1) return [CCAlert showMessage:@"请填写游戏角色ID" hidden:nil];
    else if (!product.roleName || [product.roleName length] < 1) return [CCAlert showMessage:@"请填写游戏角色名称" hidden:nil];
    else if (!product.roleLevel ||  [product.roleLevel length] < 1 || ![product.roleLevel isNumber]) return [CCAlert showMessage:@"请正确填写游戏角色等级" hidden:nil];
    _JTWZFManager = [DDZFManager manager];
    [CCHud startHud];
    _JTWZFManager.model = nil;
    _JTWZFManager.product = product;
    [_JTWZFManager.request dd_prepareZFRequest:product];
}
#pragma mark - JTWRequestDelegate
- (void)dd_didRequestWithReceiveData:(NSDictionary *)dict error:(NSError *)error
{
    [CCHud stopHud];
    if(error)
    {
        if ((error.code != -999 || ![error.localizedDescription isEqualToString:@"cancelled"]))
            [CCAlert showMessage:[NSString stringWithFormat:@"%@",error.localizedDescription] hidden:nil];
        return;
    }
    if (!dict)
        return;
    int code = -1;
    code = [[dict valueForKey:@"code"] intValue];
    NSString *info = [NSString stringWithFormat:@"%@",[dict valueForKey:@"info"]];
    if (code == 0)
    {
        if (self.request.type == JTWRequestTypePrepareZF)
        {
            self.model = [DDZFModel modelWithDict:dict];
            if ( ([self.model.fs count] == 1 && [self.model.fs[0].ID isEqualToString:@"3"]) || (![[DDHelper help].ish5 isEqualToString:HTMLGAME] && ([[DDHelper help].audistate isEqualToString:@"1"] || [[DDHelper help].country isEqualToString:@"美国"] || [[DDHelper help].country length] < 1)) )
            {
                self.product.couponID = @"0";
                self.product.balance = @"0";
                self.product.couponval = @"0";
                self.product.realPrice = self.product.goodsPrice;
                self.product.ZFChannel = @"3";
                [DDZFManager dd_managerZF:self.product web:nil];
            }
            else
            {
                [DDZFViewController dd_show:self.model product:self.product];
            }
        }
        else if (self.request.type == JTWRequestTypeZF)
        {
            if ([self.product.ZFChannel intValue] == 0)
            {
                if (self.webBlock)
                    self.webBlock(@"");
            }
            else if ([self.product.ZFChannel intValue] == 1 || [self.product.ZFChannel intValue] == 2)
            {
                if (self.webBlock)
                    self.webBlock([dict valueForKey:@"weburl"]);
            }
            else if ([self.product.ZFChannel intValue] == 3)
            {
                NSString *jiaoyihao = [dict valueForKey:@"payid"];
                if ([jiaoyihao length] <= 0)
                    return [CCAlert showMessage:@"未获取到交易号" hidden:nil];
                self.product.jiaoyiHao = jiaoyihao;
                [CCHud startHud];
                [TCCIAP dd_zf:self.product.jiaoyiHao goodsID:self.product.goodsID];
            }
        }
    }
    else
    {
        [CCAlert showMessage:[NSString stringWithFormat:@"%@" ,info] title:@"错误" enter:nil];
    }
}
#pragma mark 通知方法
- (void)dd_actionIAP:(NSNotification *)notify
{
    NSString *info = [notify.userInfo valueForKey:@"info"];
    if (info  && ![[notify.userInfo valueForKey:@"info"] isEqualToString:TCCIAPNotificationWillRequest])
    {
        [CCHud stopHud];
        [CCAlert showMessage:info title:nil enter:^{
            if ([info isEqualToString:TCCIAPNotificationSuccessRequest])
                [CCNotice post:DDPlatformPayNotification obj:nil info:@{@"message":info}];
            else
                [CCNotice post:DDPlatformCancelNotification obj:nil info:nil];
            UIViewController *VC = [UIViewController presentedViewController:nil];
            [VC dismissViewControllerAnimated:YES completion:nil];
        }];
    }
}

@end
